package com.ktjy.t382.ch01.service.imp;

import com.ktjy.t382.ch01.entity.SysUser;
import com.ktjy.t382.ch01.mapper.SysUserMapper;
import com.ktjy.t382.ch01.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class SysUserServiceImp implements SysUserService {
    @Resource
    SysUserMapper sysUserMapper;
    @Override
    public SysUser select(SysUser sysUser) {
        return sysUserMapper.select(sysUser);
    }

    @Override
    public SysUser login(SysUser sysUser) {
        return sysUserMapper.login(sysUser);
    }

    @Override
    public BigDecimal getBalance(String cradon) {
        return sysUserMapper.getBalance(cradon);
    }

    @Override
    public int update(SysUser account) {
        return sysUserMapper.update(account);
    }

    @Override
    public String transfer(String name1, String name2, BigDecimal balance) {
        if (sysUserMapper.getSelectCount(name2) != 0) {
            BigDecimal balance1 = sysUserMapper.getBalance(name1);
            if (balance1.compareTo(balance) > 0) {
                int deduction = sysUserMapper.deduction(name1, balance);
                int profit = sysUserMapper.Profit(name2, balance);
                if (deduction != 0 && profit != 0) {
                    int i = sysUserMapper.insert1(name1, balance, sysUserMapper.getBalance(name1), "转账");
                    int i1 = sysUserMapper.insert2(name2, balance, sysUserMapper.getBalance(name2), "转账");
                    if (i != 0 && i1 != 0) {
                        return "转账成功";
                    }
                    return "转账失败";
                }
                return "转账失败";
            }
            return "余额不足";
        }
        return "账号不存在";
    }

    @Override
    public String getPassword(String cradon) {
        return sysUserMapper.getPassword(cradon);
    }
}
